﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player_Controller : MonoBehaviour
{
    CharacterController characterController;
    public float speed=3;
    public Transform shoter;

    private PlayerInfo playerInfo;
    private float full_blood = 0;


    private float aimedTime=0;
    //public float 
    // Start is called before the first frame update
    void Start()
    {
        playerInfo = new PlayerInfo(50, 2, 2, 1, 0.5f);
        full_blood = playerInfo.Blood;
        GameInfo.Instance.SetPlayerForce(playerInfo.Force);
        GameInfo.Instance.SetPlayerFullBlood(playerInfo.Blood);
        UpdateMyPos();
        characterController = GetComponent<CharacterController>();
        InvokeRepeating("UpdateMyPos", 0, 0.5f);
        GameInfo.Instance.SetAndroidAim(transform);

    }

    

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKey(KeyCode.W))
        {
            characterController.Move(transform.forward * speed * Time.deltaTime);
        }
        if (Input.GetKey(KeyCode.A))
        {
            characterController.Move(-transform.right * speed * Time.deltaTime);
        }
        if (Input.GetKey(KeyCode.S))
        {
            characterController.Move(-transform.forward * speed * Time.deltaTime);
        }
        if (Input.GetKey(KeyCode.D))
        {
            characterController.Move(transform.right * speed * Time.deltaTime);
        }
        if(Input.GetMouseButtonDown(0))
        {
            Shoot();
        }

        if (playerInfo.Blood < full_blood && Time.time - aimedTime > playerInfo.RebloodTime)
        {
            aimedTime = Time.time;
            playerInfo.Blood += 1;
            if (playerInfo.Blood > full_blood)
            {
                playerInfo.Blood = full_blood;
            }
            GameInfo.Instance.onHpChange(playerInfo.Blood);
        }
    }

    void UpdateMyPos()
    {
        GameInfo.Instance.SetPlayerPos(transform.position);
    }
    void Shoot()
    {
        Transform obj = GameInfo.Instance.PlayerBulletPool.Dequeue();
        Debug.Log(GameInfo.Instance.PlayerBulletPool.Count);
        Debug.Log(shoter.name);
        obj.position = shoter.position;
        obj.rotation = shoter.rotation;
        obj.gameObject.SetActive(true);
        obj.GetComponent<Rigidbody>().WakeUp();
        obj.GetComponent<Rigidbody>().AddForce(obj.up*1000);        
    }

    private void OnCollisionEnter(Collision collision)
    {
        //Debug.Log(collision.gameObject.name);
        if (collision.gameObject.tag == "bullet_android")
        {
            collision.gameObject.GetComponent<BulletController>().Aimed();
            if (playerInfo.Blood > 0)
            {
                aimedTime = Time.time;
                playerInfo.Blood -= GameInfo.Instance.GetPlayerForce();
                GameInfo.Instance.onHpChange(playerInfo.Blood);
                if (playerInfo.Blood <= 0)
                {
                    Debug.Log("You are lose...");
                }
            }
        }
        if (collision.gameObject.tag == "bullet_player")
        {
            collision.gameObject.GetComponent<BulletController>().Aimed();
        }
    }
}
